package kawa.co.jp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;

public class TwitterMain extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		// セッションからTwitterオブジェクトを取得
		Twitter twitter = (Twitter) req.getSession().getAttribute("twitter");

		try
		{
			// アクセストークンを取得
			AccessToken accToken = twitter.getOAuthAccessToken();
			res.getWriter().println("AccessToken       = " + accToken.getToken());
			res.getWriter().println("AccessTokenSeclet = " + accToken.getTokenSecret());

			// つぶやいてみる
			java.util.Date date = new java.util.Date();
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat();
			sdf.applyPattern("yyyy'年'MM'月'dd'日 'hh'時'mm'分'ss'秒'");

			Status status = twitter.updateStatus("piromu:" + sdf.format(date) + "のつぶやき～");
			res.getWriter().println("piromu:" + status.getText());
		}
		catch (TwitterException e)
		{
			throw new ServletException(e);
		}
	}

}
